package com.example.webserver.service.impl;

import com.example.webserver.entity.Category;
import com.example.webserver.exception.ServiceException;
import com.example.webserver.mapper.Categorymapper;
import com.example.webserver.service.CategoryService;
import com.example.webserver.utils.ThreadLocalUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class Categoryserviceimpl implements CategoryService {

    @Autowired
    Categorymapper categorymapper;
    @Override
    public void add(Category category) {
        Map<String,Object> map=ThreadLocalUtil.get();
        int id= (int) map.get("id");
        category.setCreate_user(id);
        System.out.println(category.getCategory_name());
        categorymapper.add(category);
    }

    @Override
    public List<Category> list() {
        Map<String,Object> map=ThreadLocalUtil.get();
        int id= (int) map.get("id");
        return categorymapper.list(id);
    }

    @Override
    public void update(Category category) {
        if(category.getId()==0){
            throw new ServiceException("修改参数错误");
        }
        categorymapper.update(category);
    }

    @Override
    public Category findcategorybyId(int id) {
       Category category=categorymapper.findcategorybyId(id);
       return category;
    }

    @Override
    public void delete(Integer id) {
        categorymapper.delete(id);
    }
}
